library(ggplot2)
library(tidyverse)
# install.packages('ragg')
library(ragg)

# 创建一个数据

data_acc <-read.csv("SF3B1metric.csv",header = T)


head(data_acc)
#   individual group value
#1    Mister 1     A    70
#2    Mister 2     A    79
#3    Mister 3     A    64
#4    Mister 4     A    92
#5    Mister 5     A    64
#6    Mister 6     A    83


# 对数据做转换
datagroup <- data_acc$group %>% unique() #"A" "B" "C" "D"

allplotdata <- tibble('group' = datagroup,
                      'individual' = paste0('empty_individual_', seq_along(datagroup)),
                      'Accuracy' = 0) %>% 
  bind_rows(data_acc) %>% arrange(group) %>% mutate(xid = 1:n()) %>% 
  mutate(angle = 90 - 360 * (xid - 0.5) / n()) %>% 
  mutate(hjust = ifelse(angle < -90, 1, 0)) %>% 
  mutate(angle = ifelse(angle < -90, angle+180, angle)) 

head(allplotdata)
# A tibble: 6 x 6
#  group individual         value   xid angle hjust
#  <chr> <chr>              <dbl> <int> <dbl> <dbl>
#1 A     empty_individual_1     0     1  87.2     0
#2 A     Mister 1              70     2  81.6     0
#3 A     Mister 2              79     3  75.9     0
#4 A     Mister 3              64     4  70.3     0
#5 A     Mister 4              92     5  64.7     0
#6 A     Mister 5              64     6  59.1     0

# 提取出空的数据，做一些调整
firstxid <- which(str_detect(allplotdata$individual, pattern = "empty_individual")) # 1 12 43 58

segment_data <- data.frame('from' = firstxid + 1,
                           'to' = c(c(firstxid - 1)[-1], nrow(allplotdata)),
                           'label' = datagroup) %>% 
  mutate(labelx = as.integer((from + to)/2))
segment_data$label<-allplotdata$group%>% unique()
#  from to label labelx
#1    2 11     A      6
#2   13 42     B     27
#3   44 57     C     50
#4   59 64     D     61


# 自定坐标轴 
coordy <- tibble('coordylocation' = seq(from = min(allplotdata$Accuracy), to = max(allplotdata$Accuracy), 10),
                 'coordytext' = as.character(round(coordylocation, 2)),
                 'x' = 1)

# 自定义坐标轴的网格
griddata <- expand.grid('locationx' = firstxid[-1], 'locationy' = coordy$coordylocation)

# 绘图
ggplot() + 
  geom_hline(yintercept = 0,lty="solid", color = "black",linewidth=0.6)+
  geom_hline(yintercept = 0.25,lty="solid", color = "grey80")+
  geom_hline(yintercept = 0.5,lty="solid", color = "grey80")+
  geom_hline(yintercept = 0.75,lty="solid", color = "grey80")+
  geom_hline(yintercept = 1,lty="solid", color = "grey80")+
  geom_bar(data = allplotdata, aes(x = xid, y = Accuracy, fill = group), stat = 'identity') + 
  geom_text(data = allplotdata %>% filter(!str_detect(individual, pattern = "empty_individual")), 
            aes(x = xid, label = individual, y = Accuracy, angle = angle, hjust = hjust),
            color="black", fontface="bold",alpha=0.6, size=2.5) + 
  scale_fill_manual(values = colour)+
  geom_segment(data = segment_data, aes(x = from, xend = to), y = -1, yend=-0) + 
  geom_text(data = segment_data, aes(x = labelx, label = label), y = -0.1) + 
  geom_text(data = coordy, aes(x = x, y = coordylocation, label = coordytext),
            color="grey", size=3 , angle=0, fontface="bold") + 
  geom_segment(data = griddata, 
               aes(x = locationx-0.5, xend = locationx + 0.5, y = locationy, yend = locationy),
               colour = "grey", alpha=0.8, size=0.6) + 
  #scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(limits = c(-1,1)) + 
  coord_polar(clip = "off") +
  theme_void() +
  theme(legend.position = 'none')+
  geom_text(data=allplotdata, x=1,y=0,label="0",color="black",size=3)+
  geom_text(data=allplotdata, x=1,y=0.25,label="0.25",color="black",size=3)+
  geom_text(data=allplotdata, x=1,y=0.50,label="0.50",color="black",size=3)+
  geom_text(data=allplotdata, x=1,y=0.75,label="0.75",color="black",size=3)+
  geom_text(data=allplotdata, x=1,y=1.00,label="1.00",color="black",size=3)
 

